package presenter.rename;

import main.InputBean;
import utils.FileBean;
import utils.TextUtils;

import java.util.Map;

public class CusRenamerImpl implements IReNamer {

    @Override
    public boolean needReName(InputBean info, FileBean bean) {
        String preFix = info.path2();
        String name = bean.name();
        if (TextUtils.equals(name, preFix)) {
            return false;
        } else {
            return name.contains(preFix);
        }
    }

    @Override
    public void reName(InputBean info, FileBean bean, Map<String, String> container) {
        String preFix = info.path2();
        String name = bean.name();
        String to = name.replace(preFix, "");
        while (!TextUtils.isEmpty(to) && to.startsWith(".")) {
            if (to.indexOf(".") == to.lastIndexOf(".")) {
                break;
            } else {
                to = to.replaceFirst(".", "");
            }
        }
        if (!TextUtils.isEmpty(to) && !to.startsWith(".")) {
            String path = String.format("%s/", bean.parent());
            String fromPath = String.format("%s%s", path, name);
            String toPath = String.format("%s%s", path, to);
            container.put(fromPath, toPath);
        }
    }
}
